package com.study.experiment02.servlet;

import com.study.experiment02.entity.UserBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/experiment02/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserBean user = (UserBean) req.getSession().getAttribute("userRetain");
        if (user == null || (!user.getName().equals(req.getParameter("name")) &&
                !user.getPwd().equals(req.getParameter("pwd")))) {
            req.setAttribute("msg", "登录出错");
            req.getRequestDispatcher("/experiment02/login.jsp").forward(req, resp);
        } else {
            resp.sendRedirect(req.getContextPath() + "/experiment02/main.jsp");
        }
    }
}
